/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.WhoSysUsuarioInterface;
import br.gov.cemaden.library.model.smp.UsuarioSistema;
import br.gov.cemaden.library.model.smp.WhoSys;
import br.gov.cemaden.library.model.smp.WhoSysUsuario;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Simone Santos
 */
public class WhoSysUsuarioDao extends GenericDao implements WhoSysUsuarioInterface {

    private Session session;

    public WhoSysUsuarioDao(Session session) {
        this.session = session;
    }

    public List<WhoSysUsuario> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<WhoSysUsuario> whoSys = null;
        try {
            Query query = session.getNamedQuery("WhoSysUsuario.findAll").setMaxResults(30);
            whoSys = (List<WhoSysUsuario>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no WhoSysUsuarioFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return whoSys;

    }

    public WhoSysUsuario findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        WhoSysUsuario whoSys = null;
        try {
            Query query = session.getNamedQuery("WhoSysUsuario.findById").setParameter("id", id);
            whoSys = (WhoSysUsuario) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no WhoSysUsuarioFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return whoSys;
    }

    public List<WhoSysUsuario> findByUsuarioSistema(UsuarioSistema usuarioSistema) {
        Transaction tx = session.beginTransaction();
        List<WhoSysUsuario> whoSy = null;
        try {
            Query query = session.getNamedQuery("WhoSysUsuario.findByUsuarioSistema").setParameter("usuarioSistema", usuarioSistema);
            whoSy = (List<WhoSysUsuario>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no WhoSysUsuarioDao.findByUsuarioSistema: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return whoSy;

    }

    public List<WhoSysUsuario> findByWhoSys(WhoSys whoSysy) {
        Transaction tx = session.beginTransaction();
        List<WhoSysUsuario> whoSy = null;
        try {
            Query query = session.getNamedQuery("WhoSysUsuario.findByWhoSys").setParameter("whoSys", whoSysy);
            whoSy = (List<WhoSysUsuario>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no WhoSysUsuarioDao.findByWhoSys: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return whoSy;
    }

    public void insert(WhoSysUsuario whoSys) {
        try {
            saveOrUpdate(this.session, whoSys);
        } catch (Exception ex) {
            Logger.getLogger(WhoSysDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void update(WhoSysUsuario whoSys) {
        try {
            saveOrUpdate(this.session, whoSys);
        } catch (Exception ex) {
            Logger.getLogger(WhoSysDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void delete(WhoSysUsuario whoSys) {
        try {
            remove(this.session, whoSys);
        } catch (Exception ex) {
            Logger.getLogger(WhoSysDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
